from odoo import fields, models, api
from odoo.exceptions import UserError
from ..setting.pe_opening_hours import OPEN_HOURS_CATE

OPENING_HOURS_TYPE=[('ever_day','每日'),('every_week','每周')]

class PeMerchantShop(models.Model):
    _name = 'pe.merchant.shop'
    _inherit = ["mail.thread", "mail.activity.mixin"]
    _description = '商户门店'
    _sql_constraints = [('unique_internal_code','UNIQUE(internal_code)','内部编码需要唯一！')]

    name = fields.Char(string='门店名称',tracking=True)
    internal_code = fields.Char(string='内部索引',tracking=True)
    internal_name = fields.Char(string='内部称谓',tracking=True)

    pe_merchant_id = fields.Many2one(string='商户', comodel_name='pe.merchant',tracking=True)
    brand_group = fields.Many2one(string='品牌分组', comodel_name='pe.brand.group',tracking=True)
    pe_industry_domain_id = fields.Many2one(string='行业', comodel_name='pe.industry.domain',tracking=True)
    pe_marketing_status = fields.Many2one(string='运营状态', comodel_name='pe.marketing.status',tracking=True)
    customized_software_exist = fields.Boolean(string='定制软件',tracking=True)
    pe_shop_image = fields.Many2many(string='图片', comodel_name='ir.attachment',tracking=True)
    online_date = fields.Datetime(string='上线日期',tracking=True)
    telephone = fields.Char(string='联系电话',tracking=True)
    res_country_id = fields.Many2one(string='国家', comodel_name='res.country',tracking=True)
    res_province_id = fields.Many2one(string='省', comodel_name='res.country.state',tracking=True)
    res_city_id = fields.Many2one(string='市', comodel_name='res.city',tracking=True)
    res_county_id = fields.Many2one(string='区/县', comodel_name='res.county',tracking=True)
    merchant_address = fields.Char(string='地址',tracking=True)
    pe_using_device_line_ids = fields.One2many(string='使用设备', comodel_name='pe.using.device.line', inverse_name='pe_merchant_shop_id',tracking=True)
    pe_software_service_line_ids = fields.One2many(string='设备服务', comodel_name='pe.software.service.line', inverse_name='pe_merchant_shop_id',tracking=True)
    remark = fields.Char(string='备注',tracking=True)

    opening_hours_type=fields.Selection(string='营业时间类型',selection=OPENING_HOURS_TYPE)
    pe_opening_hours_ids = fields.One2many(string='营业时间', comodel_name='pe.opening.hours', inverse_name='pe_merchant_shop_id',tracking=True)

    """
    Native Function
    """
    @api.model_create_single
    def create(self, vals):
        vals['code'] = self.env['ir.sequence'].next_by_code('pe.merchant.shop') or '/'
        return super(PeMerchantShop, self).create(vals)


    @api.constrains('opening_hours_type','pe_opening_hours_ids')
    def constraint_opening_hours(self):
        for record in self:
            if record.opening_hours_type:
                if record.opening_hours_type==OPENING_HOURS_TYPE[0][0]:
                    target_record=record.pe_opening_hours_ids.filtered(lambda r:r.open_hours_cate==OPEN_HOURS_CATE[0][0])
                    if len(target_record)!=1 and len(record.pe_opening_hours_ids)!=1:
                        raise UserError('请按照规定格式填写!')
                if record.opening_hours_type==OPENING_HOURS_TYPE[1][0]:
                    target_record = record.pe_opening_hours_ids.filtered(lambda r: r.open_hours_cate == OPEN_HOURS_CATE[0][0])
                    if len(target_record)==1:
                        raise UserError('请按照规定格式填写!')
                    for item in range(1,8):
                        target_record = record.pe_opening_hours_ids.filtered(lambda r: r.open_hours_cate == OPEN_HOURS_CATE[item][0])
                        if len(target_record) != 1:
                            raise UserError('请按照规定格式填写!')

    """
    Onchange
    """
    @api.onchange('opening_hours_type')
    def onchange_on_opening_hours_type(self):
        if self.opening_hours_type:
            self.write({'pe_opening_hours_ids':False})






class PeUsingDeviceLine(models.Model):
    _name = 'pe.using.device.line'
    _description = '使用设备明细'

    code = fields.Char(string='编码')
    pe_merchant_shop_id = fields.Many2one(string='主表', comodel_name='pe.merchant.shop')
    tag_name = fields.Char(string='标记名称')
    pe_device_id = fields.Many2one(string='设备', comodel_name='pe.device.assets')
    online_date = fields.Date(string='上线日期')
    install_way = fields.Char(string='安装方式')

    @api.model_create_single
    def create(self, vals):
        vals['code'] = self.env['ir.sequence'].next_by_code('pe.using.device.line') or '/'
        return super(PeUsingDeviceLine, self).create(vals)




class PeSoftwareServiceLine(models.Model):
    _name = 'pe.software.service.line'
    _description = '软件服务表'

    code = fields.Char(string='编码')
    pe_merchant_shop_id = fields.Many2one(string='主表', comodel_name='pe.merchant.shop')
    pe_software_assets_id = fields.Many2one(string='软件', comodel_name='pe.software.assets')
    pe_version_id = fields.Many2one(string='版本', comodel_name='pe.version')
    last_update_time = fields.Datetime(string='最后更新时间')
    standard_domain = fields.Char(string='标准域名')
    username = fields.Char(string='账号')
    password = fields.Char(string='密码')
    customized_domain_exist = fields.Boolean(string='存在定制', default=False)

    @api.model_create_single
    def create(self, vals):
        vals['code'] = self.env['ir.sequence'].next_by_code('pe.software.service.line') or '/'
        return super(PeSoftwareServiceLine, self).create(vals)
